package com.ly.common.core.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @author luoyun
 * @Date: 2025/6/09 0:52
 * @Note:
 */
@Slf4j
@Aspect
@Component
public class ResTimeAop {

    /**
     * 全局接口时间响应
     *
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("@within(org.springframework.web.bind.annotation.RestController) " +
            "|| @within(org.springframework.stereotype.Controller) " +
            "|| @annotation(com.ly.common.core.annotation.ResTime)")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        // 执行目标方法
        Object result = joinPoint.proceed();
        // 计算耗时
        long endTime = System.currentTimeMillis();
        long responseTime = endTime - startTime;
        String className = joinPoint.getSignature().getDeclaringTypeName(); // 全限定类名（包名 + 类名）
        String methodName = joinPoint.getSignature().getName(); // 方法名
        String fullMethodPath = className + "." + methodName;
        log.info("[全局拦截] 接口响应时间: {}ms ---------- 方法为: {}", responseTime, fullMethodPath);
        // 不显示类全路径
        // String shortName = joinPoint.getSignature().toShortString();
        // log.info("[全局拦截] 接口响应时间: {}ms ---------- 方法为: {}", responseTime, shortName);
        return result;
    }
}